<?php


namespace app\admin\controller;

use app\admin\validate\BannerValidate;
use app\admin\validate\IDMustBePositiveInt;
use app\service\model\BannerImagesModel;
use app\service\model\BannerSeatModel;
use app\service\Y;
use think\Request;

class Banner extends BaseAdmin
{

    /**
     * banner位列表
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function seat(Request $request): \think\response\Json
    {
        $data = BannerSeatModel::paginate([
            'list_rows'=> input('param.limit', 10),
            'query' => input('param.')
        ])->toArray();
        return Y::table($data['data'], $data['total']);
    }

    /**
     * banner位列表
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function seat_list(): \think\response\Json
    {
        $data = BannerSeatModel::select();
        return Y::success($data, 'banner位列表');
    }

    /**
     * 创建、编辑banner位
     * @param Request $request
     * @param BannerValidate $banner_validate
     * @return \think\response\Json
     * @throws \app\service\exception\ParameterException
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function seat_save(Request $request, BannerValidate $banner_validate): \think\response\Json
    {
        $id = input('param.id', 0);

        if ($request->isPost()) {
            $banner_validate->goCheck('seat');
            $param = $banner_validate->getDataByRule();

            return BannerSeatModel::saveData($param, $id);
        }

        $data = [];
        if ($id > 0) {
            $data = BannerSeatModel::find($id);
        }
        return Y::success($data, 'banner位数据');
    }

    /**
     * 删除数据
     * @param IDMustBePositiveInt $IDMustBePositiveInt
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function delete(IDMustBePositiveInt $IDMustBePositiveInt): \think\response\Json
    {
        $IDMustBePositiveInt->goCheck();
        $id = input('param.id');
        return $this->deleteDate((new BannerSeatModel()), $id);
    }

    /**
     * banner位列表
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function images(Request $request): \think\response\Json
    {
        $data = BannerImagesModel::with('seat')->paginate([
            'list_rows'=> input('param.limit', 10),
            'query' => input('param.')
        ])->toArray();
        return Y::table($data['data'], $data['total']);
    }

    /**
     * 创建、编辑banner位
     * @param Request $request
     * @param BannerValidate $banner_validate
     * @return \think\response\Json
     * @throws \app\service\exception\ParameterException
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function image_save(Request $request, BannerValidate $banner_validate): \think\response\Json
    {
        $id = input('param.id', 0);

        if ($request->isPost()) {
            $banner_validate->goCheck('image');
            $param = $banner_validate->getDataByRule();

            return BannerImagesModel::saveData($param, $id);
        }

        $data = [];
        if ($id > 0) {
            $data = BannerImagesModel::find($id);
        }
        return Y::success($data, 'banner位数据');
    }

    /**
     * 删除数据
     * @param IDMustBePositiveInt $IDMustBePositiveInt
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function delete_image(IDMustBePositiveInt $IDMustBePositiveInt): \think\response\Json
    {
        $IDMustBePositiveInt->goCheck();
        $id = input('param.id');
        return $this->deleteDate((new BannerImagesModel()), $id);
    }
}